/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.pedidos.servidor.util;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;

/**
 *
 * @author Voicer02
 */
public class Monetario {    
    public static BigDecimal somar(BigDecimal x, BigDecimal y){
        BigDecimal v1 = x;
        BigDecimal v2 = y;
        BigDecimal result;
        
        result = v1.add(v2);
        result = result.setScale(2, RoundingMode.HALF_UP);
        
        return result;
    }
    
    public static BigDecimal subtrair(BigDecimal x, BigDecimal y){
        BigDecimal v1 = x;
        BigDecimal v2 = y;
        BigDecimal result;
        
        result = v1.subtract(v2);
        result = result.setScale(2, RoundingMode.HALF_UP);
        
        return result;
    }
    
    public static BigDecimal dividir(BigDecimal x, BigDecimal y){
        BigDecimal v1 = x;
        BigDecimal v2 = y;
        BigDecimal result;
        
        result = v1.divide(v2);
        result = result.setScale(2, RoundingMode.HALF_UP);
        
        return result;
    }
    
    public static BigDecimal multiplicar(BigDecimal x, BigDecimal y){
        BigDecimal v1 = x;
        BigDecimal v2 = y;
        BigDecimal result;
        result = v1.multiply(v2);
        result = result.setScale(2, RoundingMode.HALF_UP);
        
        return result;
    }
    
    public static void main(String[] args){
        BigDecimal v1 = new BigDecimal(23.12);
        BigDecimal v2 = new BigDecimal(23.12);
        BigDecimal result;
        String r;
		
        result = somar(v1, v2);
        r = Monetario.toMoney(result);
        System.out.println("Resultado MAIN: "+ r);
        result = subtrair(v1, v2);
        r = Monetario.toMoney(result);
        System.out.println("Resultado MAIN: "+ r);
        result = multiplicar(v1, v2);
        r = Monetario.toMoney(result);
        System.out.println("Resultado MAIN: "+ r);
        result = dividir(v1, v2);
        r = Monetario.toMoney(result);
        System.out.println("Resultado MAIN: "+ r);
        
    }
    
    public static String toMoney(BigDecimal v){
        
        DecimalFormat df = new DecimalFormat("###0.00");  
	String d = df.format(v);
        
        return "R$ "+d;        
    }
}
